Completed
Push — master ( 1b782b...279c80 )
by Stefan
02:36
created

Chat.componentWillUnmount   A

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
1
import React, { Component } from 'react';
2
import io from 'socket.io-client';
3
4
5
class Chat extends Component {
6
7
    constructor() {
8
        super();
9
        this.state = {
10
            history: [],
11
            log: [],
12
            latestChat: [],
13
            msg: "",
14
            tmpNick: "",
15
            userNick: ""
16
        };
17
        // this.socket = io('http://localhost:8000');
18
        this.socket = io('https://socket-server.listrom.me');
19
        this.myChangeHandler = this.myChangeHandler.bind(this);
20
        this.myChangeHandlerTwo = this.myChangeHandlerTwo.bind(this);
21
    }
22
23
    myChangeHandlerTwo = (event) => {
24
        let tmpNick = event.target.value;
25
        this.setState({tmpNick: tmpNick});
26
    }
27
28
    setUserNick = () => {
29
        this.setState({userNick: this.state.tmpNick})
30
        this.socket.emit('chat message', this.state.tmpNick + " just joined the chat")
31
    }
32
33
    myChangeHandler(event) {
34
        this.setState({msg: event.target.value});
35
    }
36
37
    sendMessage = () => {
38
        if (this.state.msg !== "") {
39
            let message = this.state.userNick + ": " + this.state.msg;
40
            this.socket.emit('chat message', message);
41
            this.setState({msg: ""});
42
        }
43
    }
44
45
    componentDidMount() {
46
        this.socket.on('connect', function() {
47
            console.log("New user connected")
48
        })
49
50
        this.socket.on('chat message', function (message) {
51
            updateChat(message);
52
            // console.log(message);
53
        });
54
55
        const updateChat = message => {
56
            this.setState({ history: [...this.state.history, message] });
57
        }
58
        fetch("https://socket-server.listrom.me/chatlog/")
59
            .then(response => response.json())
60
            .then(data => {
61
                // this.setState({ data: data.data.text});
62
                // console.log(data);
63
                for (let key in data) {
64
                    if (data.hasOwnProperty(key)) {
65
                        this.setState({ log: [...this.state.log, data[key].message] });
66
                        // console.log(key + " -> " + data[key].message);
67
                        // console.log(this.state.log);
68
                    }
69
                }
70
            });
71
    }
72
73
    scrollChat() {
74
        let chatwindow = document.getElementById("all-messages");
75
        chatwindow.scrollTop = chatwindow.scrollHeight;
76
    }
77
78
    componentDidUpdate() {
79
        if (this.state.history.length > 1) {
80
            let chatwindow = document.getElementById("all-messages");
81
            chatwindow.scrollTop = chatwindow.scrollHeight;
82
        }
83
    }
84
85
    componentWillUnmount() {
86
        this.socket.disconnect();
87
    }
88
89
    render() {
90
91
        if (this.state.userNick === "") {
92
            return (
93
                <main>
94
                    <h1>Websocket chat</h1>
95
96
                    <p><strong>Choose username:</strong></p>
97
                    <input className="new-message" value={this.tmpNick} onChange={this.myChangeHandlerTwo} />
98
                    <button className='button' onClick={this.setUserNick} >Join chat</button>
99
                </main>
100
            );
101
        } else {
102
            return (
103
                <main>
104
                    <h1>Websocket chat</h1>
105
106
                    <div
107
                        id="all-messages"
108
                        className="all-messages">
109
                        {this.state.log.map(item => (
110
                            <p key={item}>{item}</p>
111
                        ))}
112
                        {this.state.history.map(item => (
113
                            <p key={item}>{item}</p>
114
                        ))}
115
                    </div>
116
117
                    <p><strong>Write new message:</strong></p>
118
                    <input
119
                        type="text"
120
                        className="new-message"
121
                        value={this.state.msg}
122
                        onChange={this.myChangeHandler}
123
                    />
124
                    <button
125
                        className='button'
126
                        onClick={this.sendMessage}
127
                    >
128
                    Send
129
                    </button>
130
                    <br />
131
                </main>
132
            );
133
        }
134
135
136
    }
137
}
138
139
export default Chat;
140